package com.example.administrator.testxingpiao.ui.detail;

import android.util.Log;

import com.example.administrator.testxingpiao.bean.GoodsDetailBean;
import com.example.administrator.testxingpiao.config.Constant;
import com.example.administrator.testxingpiao.httputils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/8/8.
 */
public class DetailModel implements DetailContract.Model {

    @Override
    public void getGoodsDetail(final String path, final int type, final DetailCallBack<GoodsDetailBean> callBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] data = HttpUtils.getData(path);
                String json = new String(data);
                try {
                    JSONObject datasObj = new JSONObject(json).getJSONObject("datas");
                    JSONObject goodsInfoObj = datasObj.getJSONObject("goods_info");
                    String goodsName = goodsInfoObj.getString("goods_name");
                    String marketPrice = goodsInfoObj.getString("goods_marketprice");
                    String costPrice = goodsInfoObj.getString("goods_costprice");
                    String saleNum = goodsInfoObj.getString("goods_salenum");
                    String goodsStorage = goodsInfoObj.getString("goods_storage");
                    String image = (String) datasObj.getJSONArray("spec_image").get(0);
                    String goodsUrl = goodsInfoObj.getString("goods_url");

                    GoodsDetailBean db = new GoodsDetailBean(goodsName,marketPrice,costPrice,saleNum,goodsStorage,image,goodsUrl);
                    String season = "";
                    if(type == Constant.TYPE_PERFORM){
                        season = goodsInfoObj.getJSONObject("spec_value").getJSONObject("3").getString("26");
                    }else if(type == Constant.TYPE_HOTEL){
                        season = datasObj.getJSONObject("hotellist").getJSONArray("goods").getJSONObject(0).getString("Name");
                        log("season=" + season);
                    }else if(type == Constant.TYPE_TICKET){
                        season = goodsInfoObj.getJSONObject("spec_value").getJSONObject("9").getString("47");
                        String box = goodsInfoObj.getJSONObject("goods_spec").getString("49");
                        db.setBox(box);
                    }
                    db.setSeason(season);
                    callBack.GetDetail(db);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public interface DetailCallBack<T>{

        void GetDetail(T t);
    }

    public void log(String msg){
        Log.i("TAG",msg);
    }
}
